package com.insurance.controller;

import com.insurance.common.Contants;
import com.insurance.common.ResultData;
import com.insurance.pojo.*;
import com.insurance.service.UserService;

import com.insurance.utils.ConversionDateUtils;
import com.insurance.utils.PasswordEncryption;
import com.insurance.utils.UploadFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;


@Api(value = "客户管理",tags = "客户管理API")
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private ResultData resultData;

    @Autowired
    private UserService userService;

    @ResponseBody
    @GetMapping(value = "/queryUser.do",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "查询用户",notes = "查询用户接口",httpMethod = "get",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData queryUser(User user){
        String regex = "^1[3|4|5|8][0-9]\\d{8}$";
        User user1 = new User();
        System.out.println("打印结果："+user);
        if (user.getUserName() == null){
            List<Map> mapList = userService.queryUser(user1);
            if (mapList.size() > 0){
                resultData.setCode(Contants.SUCCESS);
                resultData.setMsg("查询成功");
                resultData.setData(mapList);
                return resultData;
            }else {
                resultData.setCode(Contants.FAILURE);
                resultData.setMsg("后台服务错误");
                return resultData;
            }
        } else if (user.getUserName() != null && user.getUserName().matches(regex)){
            user1.setUserPhone(user.getUserName());
            List<Map> mapList = userService.queryUser(user1);
            if (mapList.size() > 0){
                resultData.setCode(Contants.SUCCESS);
                resultData.setMsg("查询成功");
                resultData.setData(mapList);
                return resultData;
            }else {
                resultData.setCode(Contants.FAILURE);
                resultData.setMsg("后台服务错误");
                return resultData;
            }
        }else {
            user1.setUserName(user.getUserName());
            List<Map> mapList = userService.queryUser(user1);
            if (mapList.size() > 0){
                resultData.setCode(Contants.SUCCESS);
                resultData.setMsg("查询成功");
                resultData.setData(mapList);
                return resultData;
            }else {
                resultData.setCode(Contants.FAILURE);
                resultData.setMsg("后台服务错误");
                return resultData;
            }
        }

    }

    @ResponseBody
    @PostMapping(value = "/addUser.do",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "添加用户",notes = "添加用户接口",httpMethod = "post",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="添加成功"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData addUser(@RequestBody User user){
        System.out.println("打印结果："+user);
        String userPwd = user.getUserPwd();
        user.setUserPwd(PasswordEncryption.passwordEncryption(userPwd));
        int result = userService.addUser(user);
        if (result > 0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("添加成功");
            return resultData;
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("后台服务错误");
            return resultData;
        }
    }

    @ResponseBody
    @PostMapping(value = "/updateUser.do",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "修改用户",notes = "修改用户接口",httpMethod = "post",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="修改成功"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData updateUser(@RequestBody User user){
        user.setUserPwd(PasswordEncryption.passwordEncryption(user.getUserPwd()));
        System.out.println("打印结果："+user);
        int result = userService.updateUser(user);
        if (result > 0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("修改成功");
            return resultData;
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("后台服务错误");
            return resultData;
        }
    }

    @ResponseBody
    @PostMapping(value = "/downloadUser.do",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "导出用户信息",notes = "导出用户信息接口",httpMethod = "post",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="导出成功"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData downloadUser(){
        resultData.setCode(Contants.SUCCESS);
        resultData.setMsg("导出成功");
        return resultData;
    }

    @ResponseBody
    @GetMapping(value = "/queryOwnerCard.do",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "查询车主卡",notes = "查询车主卡接口",httpMethod = "get",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=500,message="查无此人")
    })
    public ResultData queryOwnerCard(OwnerCard ownerCard){
        String regex = "^1[3|4|5|8][0-9]\\d{8}$";
        OwnerCard ownerCard1 = new OwnerCard();
        try{
            ownerCard1.setOwnerFirstDate2(ConversionDateUtils.conversionDateUtils(ownerCard.getOwnerFirstDate()));//将初登日期转换为时间戳
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("打印结果："+ownerCard);
         if (ownerCard.getUserName() != null && ownerCard.getUserName().matches(regex)){//根据电话和初登日期时进行精准查询
            ownerCard1.setUserPhone(ownerCard.getUserName());
            List<Map> mapList = userService.queryOwnerCard(ownerCard1);
            if (mapList.size()>0){
                resultData.setCode(Contants.SUCCESS);
                resultData.setMsg("查询成功");
                resultData.setData(mapList);
                return resultData;
            }else {
                resultData.setCode(Contants.FAILURE);
                resultData.setMsg("查无此人");
                return resultData;
            }
         }else if (ownerCard.getUserName() != null){//根据名字和初登日期进行精准查询
             ownerCard1.setUserName(ownerCard.getUserName());
             List<Map> mapList = userService.queryOwnerCard(ownerCard1);
             if (mapList.size()>0){
                 resultData.setCode(Contants.SUCCESS);
                 resultData.setMsg("查询成功");
                 resultData.setData(mapList);
                 return resultData;
             }else {
                 resultData.setCode(Contants.FAILURE);
                 resultData.setMsg("查无此人");
                 return resultData;
             }
         } else {//查询所有或查询只有初登日期的数据
             List<Map> mapList = userService.queryOwnerCard(ownerCard1);
             if (mapList.size()>0){
                 resultData.setCode(Contants.SUCCESS);
                 resultData.setMsg("查询成功");
                 resultData.setData(mapList);
                 return resultData;
             }else {
                 resultData.setCode(Contants.FAILURE);
                 resultData.setMsg("查无此人");
                 return resultData;
             }
         }
    }

    @ResponseBody
    @PostMapping(value = "/addOwnerCard.do",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "添加车主卡",notes = "添加车主卡接口",httpMethod = "post",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="添加成功"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData addOwnerCard(@RequestBody OwnerCard ownerCard) throws IOException, FileUploadException {
        System.out.println("打印结果："+ownerCard);
        try{
            ownerCard.setOwnerFirstDate2(ConversionDateUtils.conversionDateUtils(ownerCard.getOwnerFirstDate()));//将初登日期转换为时间戳
        }catch (Exception e){
            e.printStackTrace();
        }
        int result = userService.addOwnerCard(ownerCard);
        if (result > 0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("添加成功");
            return resultData;
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("后台服务错误");
            return resultData;
        }
    }

    @ResponseBody
    @PostMapping(value = "/uploadFile.do",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "文件上传",notes = "文件上传接口",httpMethod = "post",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="上传成功"),
    })
    public ResultData uploadFile(MultipartFile[] multipartFiles, HttpServletRequest request) throws IOException, FileUploadException {
        try{
            String[] src = UploadFile.uploadFile(request,multipartFiles);
            String src1 = "";
            for (String src2:src) {
                src1 +=" " + src2;
            }
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("上传成功");
            resultData.setData(src1);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultData;
    }

    @ResponseBody
    @PostMapping(value = "/updateOwnerCard.do",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "修改车主卡",notes = "修改车主卡接口",httpMethod = "post",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="修改成功"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData updateOwnerCard(@RequestBody OwnerCard ownerCard, HttpServletRequest request) throws IOException {
        System.out.println("打印结果："+ownerCard);
        try{
            ownerCard.setOwnerFirstDate2(ConversionDateUtils.conversionDateUtils(ownerCard.getOwnerFirstDate()));//将初登日期转换为时间戳
            MultipartFile[] multipartFiles = ownerCard.getFile();
            String[] src = UploadFile.uploadFile(request,multipartFiles);
            String src1 = null;
            for (String src2:src) {
                src1 +="," + src2;
            }
            ownerCard.setVehicleLicenseAddress(src1);
        }catch (Exception e){
            e.printStackTrace();
        }
        int result = userService.updateOwnerCard(ownerCard);
        if (result > 0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("修改成功");
            return resultData;
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("后台服务错误");
            return resultData;
        }
    }

    @ResponseBody
    @GetMapping(value = "/queryBrand.do",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "查询品牌",notes = "查询品牌接口",httpMethod = "get",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData queryBrand(Brand brand){
        System.out.println("打印结果："+brand);
        List<Map> mapList = userService.queryBrand(brand);
        if (mapList.size() > 0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("查询成功");
            resultData.setData(mapList);
            return resultData;
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("后台服务错误");
            return resultData;
        }
    }

    @ResponseBody
    @GetMapping(value = "/queryCarModel.do",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "查询车辆型号",notes = "查询车辆型号接口",httpMethod = "get",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData queryCarModel(CarModel carModel){
        System.out.println("打印结果："+carModel);
        List<Map> mapList = userService.queryCarModel(carModel);
        if (mapList.size() > 0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("查询成功");
            resultData.setData(mapList);
            return resultData;
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("后台服务错误");
            return resultData;
        }
    }

    @ResponseBody
    @GetMapping(value = "/queryCarSpecificModels.do",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "查询车辆具体型号",notes = "查询车辆具体型号接口",httpMethod = "get",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData queryCarSpecificModels(CarSpecificModels carSpecificModels){
        System.out.println("打印结果："+carSpecificModels);
        List<Map> mapList = userService.queryCarSpecificModels(carSpecificModels);
        if (mapList.size() > 0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("查询成功");
            resultData.setData(mapList);
            return resultData;
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("后台服务错误");
            return resultData;
        }
    }

    @ResponseBody
    @GetMapping(value = "/queryDriver.do",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "查询司机",notes = "查询司机接口",httpMethod = "get",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData queryDriver(Driver driver){
        String regex = "^1[3|4|5|8][0-9]\\d{8}$";
        Driver driver1 = new Driver();
        System.out.println("打印信息："+driver);
        if (driver.getUserName() == null){
            List<Map> mapList = userService.queryDriver(driver1);
            if (mapList.size() > 0){
                resultData.setCode(Contants.SUCCESS);
                resultData.setMsg("查询成功");
                resultData.setData(mapList);
                return resultData;
            }else {
                resultData.setCode(Contants.FAILURE);
                resultData.setMsg("后台服务错误");
                return resultData;
            }
        } else if (driver.getUserName() != null && driver.getUserName().matches(regex)){
            driver1.setUserPhone(driver.getUserName());
            List<Map> mapList = userService.queryDriver(driver1);
            if (mapList.size() > 0){
                resultData.setCode(Contants.SUCCESS);
                resultData.setMsg("查询成功");
                resultData.setData(mapList);
                return resultData;
            }else {
                resultData.setCode(Contants.FAILURE);
                resultData.setMsg("后台服务错误");
                return resultData;
            }
        }else {
            driver1.setUserName(driver.getUserName());
            List<Map> mapList = userService.queryDriver(driver1);
            if (mapList.size() > 0){
                resultData.setCode(Contants.SUCCESS);
                resultData.setMsg("查询成功");
                resultData.setData(mapList);
                return resultData;
            }else {
                resultData.setCode(Contants.FAILURE);
                resultData.setMsg("后台服务错误");
                return resultData;
            }
        }
    }

    @ResponseBody
    @PostMapping(value = "/addDriver.do",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "添加司机",notes = "添加司机接口",httpMethod = "post",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="添加成功"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData addDriver(@RequestBody Driver driver){
        System.out.println("打印结果："+driver);
        int result = userService.addDriver(driver);
        if (result > 0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("添加成功");
            return resultData;
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("后台服务错误");
            return resultData;
        }
    }

    @ResponseBody
    @PostMapping(value = "/updateDriver.do",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "修改司机",notes = "修改司机接口",httpMethod = "post",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="修改成功"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData updateDriver(@RequestBody Driver driver){
        System.out.println("打印结果："+driver);
        int result = userService.updateDriver(driver);
        if (result > 0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("修改成功");
            return resultData;
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("后台服务错误");
            return resultData;
        }
    }

    @ResponseBody
    @PostMapping(value = "/delDriver.do",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "删除司机",notes = "删除司机接口",httpMethod = "post",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="删除成功"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData delDriver(@RequestBody Driver driver){
        System.out.println("打印结果："+driver);
        int result = userService.delDriver(driver);
        if (result > 0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("删除成功");
            return resultData;
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("后台服务错误");
            return resultData;
        }
    }



}